import MyUtil.DataBaseUtil;
import MyUtil.MyPanel;
import MyUtil.RedisUtil;
import redis.clients.jedis.Jedis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class LoginView extends JFrame{
    private CardLayout  cardLayout = new CardLayout();
    private Connection conn;
    private JLabel jlabel = new JLabel("登录界面");// 标签
    private JTextField eidText =  new JTextField(10);; // 账号域
    private JPasswordField epasText =new JPasswordField(10); //密码域

    private JLabel eid_label = new JLabel("账号");
    private JLabel epassword_label = new JLabel("密码");
    private int IsManager;//是否是管理员   0 账号错误或密码错误   1为销售员  2为系统管理员
    private String EmployeeName;//员工姓名
    //按钮
    private JButton login_button = new JButton("登录");//登录按钮

    private MyPanel myPanel; // 组件
    public LoginView(){
        IsManager = 0;//默认为否
        myPanel = new MyPanel("/picture/2.jpg");

        this.setTitle("登录界面");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(800,300,400,400);

        this.setContentPane(myPanel);
        myPanel.setLayout(new CustomerLayout());

        conn = DataBaseUtil.GetConnection();

        myPanel.add(jlabel);
        myPanel.add(eidText);
        myPanel.add(epasText);
        myPanel.add(login_button);
        myPanel.add(eid_label);
        myPanel.add(epassword_label);
        //图标设置
        jlabel.setIcon(new ImageIcon("/picture/ic_open.png"));
        login_button.setIcon(new ImageIcon("D:\\数据库课程设计\\src\\picture\\4.jpg"));
        /*login_button.setContentAreaFilled(false);
        login_button.setFocusPainted(false);*/
        epasText.setEchoChar('*');
        epasText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent ee) {
                //KeyEvent.VK_ENTER：等价于回车键按下
                if (ee.getKeyCode() == KeyEvent.VK_ENTER) {
                    //自定义方法卡号和密码是否存在
                    try {
                        IsManager = epasText_method();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if(IsManager==1){
                        //卡号和密码正确
                        // TODO 根据不同权限跳转页面
                        setVisible(false);//当前窗口不可见
                        new Saleman_View(EmployeeName,eidText.getText());
                    }else if(IsManager==2){
                        setVisible(false);//当前窗口不可见
                        new Manager_View(EmployeeName,eidText.getText());
                    }else{
                        //卡号或密码不正确
                        //JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框
                        JOptionPane.showMessageDialog(null, "用户名不存在或密码不正确", "登陆错误",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        login_button.addActionListener((e)->{
            try {
                IsManager = epasText_method();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            if(IsManager==1){
                //卡号和密码正确
                // TODO 根据不同权限跳转页面
                setVisible(false);//当前窗口不可见
                new Saleman_View(EmployeeName,eidText.getText());
            }else if(IsManager==2){
                setVisible(false);//当前窗口不可见
                new Manager_View(EmployeeName,eidText.getText());
            }
            else{
                //卡号或密码不正确
                //JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框
                JOptionPane.showMessageDialog(null, "用户名不存在或密码不正确", "登陆错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
        this.setVisible(true);
    }
    public int epasText_method() throws SQLException {
        String eid=String.valueOf(eidText.getText());
        String epas=String.valueOf(epasText.getPassword());
        System.out.println(eid+" "+epas);
        Jedis jedis = RedisUtil.getConRedis();
        List<String> list = jedis.lrange(eid, 0 ,-1);
        try{
            if(epas .equals(list.get(0))){
                EmployeeName = list.get(2);
                return Integer.parseInt(list.get(1));
            }
        }catch (Exception e){
            System.out.println("redis里没照到");
            String sql = "select Ename,Elevel from Employee where Eaccount=? and Epassword = ?";
            PreparedStatement p1 = conn.prepareStatement(sql);
            p1.setString(1,eid);
            p1.setString(2,epas);
            ResultSet rs = p1.executeQuery();
            if(rs.next()){
                EmployeeName =rs.getString("Ename");
                return rs.getInt("Elevel");
            }
        }

        return 0;
    }
    private class CustomerLayout implements LayoutManager{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return null;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return null;
        }

        @Override
        public void layoutContainer(Container parent) {
            if (jlabel.isVisible()){
                // 标签
                jlabel.setBounds(170,10,100,50);
            }
            if(eid_label.isVisible()){
                eid_label.setBounds(40,100,30,30);
            }
            if(epassword_label.isVisible()){
                epassword_label.setBounds(40,170,30,30);
            }
            if(eidText.isVisible()){
                // 账号域
                eidText.setBounds(90,100,220,30);
            }
            if(epasText.isVisible()){
                //密码域
                epasText.setBounds(90,170,220,30);
            }
            if(login_button.isVisible()){
                login_button.setBounds(140,230,110,80);
            }
        }
    }
}
